package com.zhejiangzhengyuan.municipal_view_demo;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lin_ju
 * @since on 2019/11/22
 */
@Configuration
@EnableSwagger2
@Slf4j
public class SwaggerApplication {


  @Value("${swagger2.enable}")
  private boolean enable;

  private final String PRE_PACKAGE = "com.zhejiangzhengyuan.";

  @Bean("municipal_common")
  public Docket municipalCommonApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("全局通用接口")
        .apiInfo(apiInfo())
        .select()
        // 为当前包路径
        .apis(RequestHandlerSelectors.basePackage(PRE_PACKAGE + "municipal_common.controller"))
        .paths(PathSelectors.any())
        .build();
  }

  @Bean("municipal_flooded_road")
  public Docket municipalFloodedRoadApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("道路积水子系统接口")
        .apiInfo(apiInfo())
        .select()
        // 为当前包路径
        .apis(
            RequestHandlerSelectors.basePackage(PRE_PACKAGE + "municipal_flooded_road.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(formatParams());
  }

  @Bean("municipal_manhole_cover")
  public Docket municipalManholeCoverApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("井盖子系统接口")
        .apiInfo(apiInfo())
        .select()
        // 为当前包路径
        .apis(
            RequestHandlerSelectors.basePackage(PRE_PACKAGE + "municipal_manhole_cover.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(formatParams());
  }

  @Bean("municipal_pumping_station")
  public Docket municipalPumpingStationApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("泵站子系统接口")
        .apiInfo(apiInfo())
        .select()
        // 为当前包路径
        .apis(
            RequestHandlerSelectors.basePackage(
                PRE_PACKAGE + "municipal_pumping_station.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(formatParams());
  }

  @Bean("municipal_sewage")
  public Docket municipalSewageApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("雨污混排子系统接口")
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage(PRE_PACKAGE + "municipal_sewage.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(formatParams());
  }

  @Bean("municipal_security")
  public Docket municipalSecurityApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("账号及鉴权相关模块")
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage(PRE_PACKAGE + "municipal_security.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(formatParams());
  }

  @Bean("municipal_way")
  public Docket municipalWayApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("一路一档子系统接口")
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage(PRE_PACKAGE + "municipal_way.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(formatParams());
  }

  @Bean("municipal_bi")
  public Docket municipalBIApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("市政大数据展示接口")
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage(PRE_PACKAGE + "municipal_bi.controller"))
        .paths(PathSelectors.any())
        .build()
        .globalOperationParameters(formatParams());
  }

  // 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        // 页面标题
        .title("智慧市政 RESTful Api文档")
        // 创建人
        .contact(new Contact("浙江正元", "zhejiangzhengyuan.com", "zjzydlxx@163.com"))
        // 版本号
        .version("1.0")
        // 描述
        .description("API 描述文档")
        .build();
  }

  private List<Parameter> formatParams() {
    ParameterBuilder tokenPar = new ParameterBuilder();
    List<Parameter> pars = new ArrayList<>();
    // 存放token参数
    tokenPar
        .name("Authorization")
        .description("令牌")
        .modelRef(new ModelRef("string"))
        .parameterType("header")
        .required(false)
        .build();
    pars.add(tokenPar.build());
    return pars;
  }
}
